﻿using System;
using System.Collections.Generic;
using System.Text;
using Samples.Helper;
using ebay.Utility;
using eBay.Service.Call;
using eBay.Service.Core.Soap;
using eBay.Service.Core.Sdk;
using eBay.Service.Util;
using System.Runtime.InteropServices;

namespace ebay.ebayApi
{
    [StructLayout(LayoutKind.Sequential)]
    public class SystemTime
    {
        public ushort Year;
        public ushort Month;
        public ushort Dayofweek;
        public ushort Day;
        public ushort Hour;
        public ushort Minute;
        public ushort Second;
        public ushort Millisecond;
    }

    public class ebayCallsApi
    {

        public ApiContext Context;

        #region 初始化 Api信息
        public ebayCallsApi(string token)
        {
            Context = AppSettingHelper.GetApiContext();//Token
            if (!string.IsNullOrEmpty(token))
            {
                Context.ApiCredential.eBayToken = token;
            }
            Context.Site = PubClass.GetEnum<eBay.Service.Core.Soap.SiteCodeType>(System.Configuration.ConfigurationManager.AppSettings.Get("UserAccount.eBayUserSiteId"));

        }

        #endregion

        #region 获取Condition
        public CategoryFeatureTypeCollection GetCondition()
        {
            GetCategoryFeaturesCall gfc = new GetCategoryFeaturesCall(Context);
            gfc.DetailLevelList.Add(eBay.Service.Core.Soap.DetailLevelCodeType.ReturnAll);
            gfc.CategoryID = "63861";

            return gfc.GetCategoryFeatures();
        }
        #endregion

        public CategoryTypeCollection GetCategory2CS()
        {
            GetCategory2CSCall apicall = new GetCategory2CSCall(Context);
            apicall.DetailLevelList.Add(eBay.Service.Core.Soap.DetailLevelCodeType.ReturnAll);

            return apicall.GetCategory2CS();
        }

        public CategoryTypeCollection GetCategory()
        {
            GetCategoriesCall apicall = new GetCategoriesCall(Context);
            apicall.EnableCompression = true;
            apicall.DetailLevelList.Add(eBay.Service.Core.Soap.DetailLevelCodeType.ReturnAll);
            apicall.ViewAllNodes = true;
            return apicall.GetCategories();
        }

        public UserType GetUser()
        {
            GetUserCall apicall = new GetUserCall(Context);
            //return apicall.GetUser();

            UserType usertype = new UserType();
            try
            {
                usertype = apicall.GetUser();
            }
            catch
            {
                usertype = null;
            }
            return usertype;
        }

        public ItemType GetItem(string itemId)
        {
            Context.CallRetry = GetCallRetry();

            GetItemCall apicall = new GetItemCall(Context);
            apicall.DetailLevelList.Add(DetailLevelCodeType.ReturnAll);

            ItemType item = apicall.GetItem(itemId);
            return item;
        }
        public string GetSessionId(string RulName)
        {
            GetSessionIDCall gsc = new GetSessionIDCall(Context);
            Context.RuName = RulName;
            return gsc.GetSessionID(Context.RuName);
        }

        public string GetToken(string sessionId)
        {
            FetchTokenCall ftc = new FetchTokenCall(Context);
            Context.ApiCredential.eBayToken = ftc.FetchToken(sessionId);
            return Context.ApiCredential.eBayToken;
        }

        private CallRetry GetCallRetry()
        {
            CallRetry retry = new CallRetry();
            retry.DelayTime = 1000;		// 1 second
            retry.MaximumRetries = 2;
            retry.TriggerHttpStatusCodes = new Int32Collection();
            retry.TriggerHttpStatusCodes.Add(502);
            retry.TriggerHttpStatusCodes.Add(404);
            return retry;
        }

       
        public DateTime ReviseItem(ItemType item, ref double listing_Fee)
        {
         
            ReviseItemCall apicall = new ReviseItemCall(Context);
            DateTime Start_date=apicall.ReviseItem(item, null, false);

            FeeTypeCollection fees = apicall.FeeList;

            foreach (FeeType fee in fees)
            {
                if (fee.Name == "ListingFee")
                {
                    listing_Fee = fee.Fee.Value;
                    break;
                }
            }
            return Start_date;          
        }

        public string EndItem(ItemType Item)
        {
            EndItemCall apicall = new EndItemCall(Context);
            apicall.EndItem(Item.ItemID, EndReasonCodeType.NotAvailable);
            return apicall.ApiResponse.Ack.ToString();        
        }

        // Load system api
        [DllImport("Kernel32.dll")]
        private static extern Boolean SetSystemTime([In, Out] SystemTime st);
        
    }
}